package com.stacktry.funtv.utils;

import android.content.Context;
import android.util.Log;

import com.fun.xm.Definition;
import com.fun.xm.FSCallback;
import com.fun.xm.FSIVideoPlayer;
import com.fun.xm.FSPlayer;
import com.fun.xm.FSVideoReqData;
import com.funshion.playsdk.constant.FSError;

import java.util.HashMap;
import java.util.List;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodChannel;

public class Player {
    //播放逻辑调度
    private FSIVideoPlayer mFunshionIVideoPlayer;

    FSVideoReqData mFSVideoReqData;
    private List<Definition> mDefinitions;
    private Definition mCurrDefinition;
    FlutterPlugin.FlutterPluginBinding flutterPluginBinding;
    public Player(FlutterPlugin.FlutterPluginBinding flutterPluginBinding){
        this.flutterPluginBinding = flutterPluginBinding;
    }
    public void createPlayer(){
        if(mFunshionIVideoPlayer != null){
            return;
        }
        try {
            mFunshionIVideoPlayer = FSPlayer.create(flutterPluginBinding.getApplicationContext(), mFSCall);
            //可以打开调试信息方便查询问题
            //mFunshionIVideoPlayer.setDebug(true);
        }catch (Exception e){}
    }

    MethodChannel.Result result;
    /**
     * 播放媒体
     */
    public synchronized void playLongMedia(HashMap<String,String> params, MethodChannel.Result result) {
        createPlayer();
        this.result = result;
        mFSVideoReqData = new FSVideoReqData(params.get("ceCode"), new Definition(Definition.CLARITY_SUPPER));
        mFSVideoReqData.setUid(params.get("uid")); // fohg4v1_dev1
        mFSVideoReqData.setPlayFlag(Integer.parseInt(params.get("flag")));
        mFSVideoReqData.setCmCode(params.get("cmCode")); // M0DBMBYKBE
        try {
            Log.d("player", "request");
            mFunshionIVideoPlayer.requestAndPrepare(mFSVideoReqData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    FSCallback mFSCall = new FSCallback() {

        @Override
        public void onDefinition(List<Definition> definitions, Definition currDefinition) {
            for (Definition definition : definitions) {
                Log.e("FE", "get Definition " + definition);
            }
            mDefinitions = definitions;
            mCurrDefinition = currDefinition;
//            Toast.makeText(MainActivity.this, "当前清晰度：" + currDefinition.getStringDefinition(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailed( FSError arg0) {
            Log.e("player", "onFailed:" + arg0);
            if(result != null){
                result.error(""+arg0.getErrorCode(),arg0.getErrorMessage(),"");
            }
        }

        public void onReceiveUrl(String url) {
            mFunshionIVideoPlayer.isTrySource();
            if (mCurrDefinition != null ){
                //Toast.makeText(MainActivity.this, "当前清晰度：" + mCurrDefinition.getStringDefinition()+"\n"+"当前是否为试看：" +mFunshionIVideoPlayer.isTrySource(), Toast.LENGTH_LONG).show();
            }
            if(result != null){
                result.success(url);
            }
            Log.d("player", "onReceiveUrl:" + url);

        }
    };


}
